package com.lyh.hkcloud.activity;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import androidx.appcompat.app.ActionBar;
import com.lyh.hkcloud.R;
import com.lyh.hkcloud.adapter.*;
import com.lyh.hkcloud.base.BaseActivity;
import com.lyh.hkcloud.constants.*;
import com.lyh.hkcloud.util.base.LoadingUtils;
import com.lyh.hkcloud.util.base.LogUtil;
import com.lyh.hkcloud.util.base.ToastUtils;
import com.lyh.hkcloud.util.http.listener.json.JsonFailuredListener;
import com.lyh.hkcloud.util.http.listener.json.JsonSuccessListener;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.lyh.hkcloud.service.*;

public class ListActivity extends BaseActivity {

    private ListView lvList;

    private TextView tvMsg;

    private AppService appService;

    private JSONArray list;

    @Override
    protected Activity getActivity() {
        return this;
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_list;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.options_child_menu, menu);
        menu.findItem(R.id.action_person).setTitle(UserInfo.psnCode);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_person:
                //showInfo();
                break;
            case R.id.action_logout:
                logout();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void initData() {
        try {
            LoadingUtils.getInstance(com.lyh.hkcloud.activity.ListActivity.this).show();
            JSONObject object = new JSONObject(getIntent().getStringExtra("object"));
            ActionBar actionBar = this.getSupportActionBar();
            assert actionBar != null;
            actionBar.setTitle(object.optString("appName"));
            actionBar.setDisplayHomeAsUpEnabled(true);
            appService.getMenus(object.optInt("appId"), new JsonSuccessListener() {
                @Override
                public void onRespone(String msg, JSONObject response) {
                    LoadingUtils.getInstance(com.lyh.hkcloud.activity.ListActivity.this).hide();
                    list = response.optJSONArray("data");
                    lvList.setAdapter(new ListViewListAdapter(com.lyh.hkcloud.activity.ListActivity.this, list));
                }
            }, new JsonFailuredListener() {
                @Override
                public void onRespone(String msg, JSONObject response) {
                    Log.e("error",response.optString("message"));
                    LoadingUtils.getInstance(com.lyh.hkcloud.activity.ListActivity.this).hide();
                    tvMsg.setText(msg);
                    tvMsg.setVisibility(View.VISIBLE);
                    lvList.setVisibility(View.GONE);
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onBeginInit() {
        appService = new AppService(com.lyh.hkcloud.activity.ListActivity.this);
    }

    @Override
    protected void initView() {
        lvList = findViewById(R.id.lv_list);
        tvMsg = findViewById(R.id.tv_msg);
    }

    @Override
    protected void initEvent() {
        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                JSONObject object = list.optJSONObject(position);
                Class<?> toClass;
                if (AppConstants.UI_WEB.equals(object.optString("UIClassName"))) {
                    toClass = com.lyh.hkcloud.activity.WebActivity.class;
                } else if (AppConstants.UI_ACTIVITY.equals(object.optString("UIClassName"))) {
                    int transactionId = object.optInt("sysFunTransactionID");
                    try {
                        toClass = Class.forName("com.lyh.hkcloud.module.m" + transactionId + ".IndexActivity");
                    } catch (ClassNotFoundException e) {
                        LogUtil.e("找不到事务码对事的activity", "事务码为：" + transactionId);
                        ToastUtils.makeText(com.lyh.hkcloud.activity.ListActivity.this, "启动该功能失败，请联系管理员！", ToastUtils.LENGTH_SHORT).show();
                        return;
                    }
                } else {
                    ToastUtils.makeText(com.lyh.hkcloud.activity.ListActivity.this, "启动该功能失败，请联系管理员！", ToastUtils.LENGTH_SHORT).show();
                    return;
                }
                Intent intent = new Intent(com.lyh.hkcloud.activity.ListActivity.this, toClass);
                intent.putExtra("object", object.toString());
                com.lyh.hkcloud.activity.ListActivity.this.startActivity(intent);
            }
        });
    }

}
